1
/****************************** Module Header ******************************\
2 * Module Name: Service2.svc.cs
3 * Project: CSAzureWCFServices
4 * Copyright (c) Microsoft Corporation.
6 * This class implements WCFService.IContract interface. Methods talks to all
7 * instances of workrole1 to return the instance name and communication channel.
8 * These data are returned to the client together with the instance name/communication
9 * channel of the current web role instance.
11 * This source is subject to the Microsoft Public License.
12 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 * All other rights reserved.
15 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
16 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
18 \***************************************************************************/
22 using System
.Runtime
.Serialization
;
23 using System
.ServiceModel
;
25 using Microsoft
.WindowsAzure
;
26 using Microsoft
.WindowsAzure
.Diagnostics
;
27 using Microsoft
.WindowsAzure
.ServiceRuntime
;
29 namespace WCFServiceWebRole1
31 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service2"
32 // in code, svc and config file together.
33 // implement the wcf contract WCFContract.IContract in a different way
34 public class Service2
: WCFContract
.IContract
37 // Return the current web role's name and instance id;
38 // Plus, this web role instance talks to a work role instance via internal endpoint, and get
39 // the work role's instance data.
40 public string GetRoleInfo()
42 RoleInstance currentRoleInstance
= RoleEnvironment
.CurrentRoleInstance
;
43 string roleName
= currentRoleInstance
.Role
.Name
;
44 string roleInstanceID
= currentRoleInstance
.Id
;
45 string thisWR
= string.Format("You are talking to the workroles via role {0}, instance ID {1}\n.",
46 roleName
, roleInstanceID
);
48 // Contact the workrole and get its info
49 string workRoleInfo
= string.Empty
;
50 System
.Text
.StringBuilder sb
= new StringBuilder();
52 var roles
= RoleEnvironment
.Roles
["WorkerRole1"];
53 foreach (var instance
in roles
.Instances
)
55 RoleInstanceEndpoint WorkRoleInternalEndPoint
= instance
.InstanceEndpoints
["Internal"];
56 NetTcpBinding binding
= new NetTcpBinding(SecurityMode
.None
, false);
57 EndpointAddress myEndpoint
= new EndpointAddress(String
.Format("net.tcp://{0}/Internal",
58 WorkRoleInternalEndPoint
.IPEndpoint
));
60 ChannelFactory
<IContract
> myChanFac
= new ChannelFactory
<WCFContract
.IContract
>(binding
, myEndpoint
);
61 WCFContract
.IContract myClient
= myChanFac
.CreateChannel();
62 sb
.Append(myClient
.GetRoleInfo() + "\n");
65 workRoleInfo
= sb
.ToString() ;
67 return (thisWR
+ "\n" + workRoleInfo
);
71 // Return the current communication channel between the external client and this web role instance;
72 // Plus, return the internal channel between this web role instance and all work role instances.
73 public string GetCommunicationChannel()
75 string thisWebRoleChannel
=string.Format("You are talking to the workroles via {0}.",
76 OperationContext
.Current
.Channel
.LocalAddress
.Uri
.ToString());
78 // Contact the workrole and get the channel info
79 string workRoleChannel
= string.Empty
;
80 System
.Text
.StringBuilder sb
= new StringBuilder();
82 var roles
= RoleEnvironment
.Roles
["WorkerRole1"];
83 foreach (var instance
in roles
.Instances
)
85 RoleInstanceEndpoint workRoleInternalEndPoint
= instance
.InstanceEndpoints
["Internal"];
86 NetTcpBinding binding
= new NetTcpBinding(SecurityMode
.None
, false);
87 EndpointAddress myEndpoint
= new EndpointAddress(String
.Format("net.tcp://{0}/Internal",
88 workRoleInternalEndPoint
.IPEndpoint
));
90 ChannelFactory
<IContract
> myChanFac
= new ChannelFactory
<WCFContract
.IContract
>(binding
, myEndpoint
);
91 WCFContract
.IContract myClient
= myChanFac
.CreateChannel();
92 sb
.Append(myClient
.GetCommunicationChannel() + "\n");
95 workRoleChannel
= sb
.ToString();
97 return (thisWebRoleChannel
+ "\n" + workRoleChannel
);